package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.bigClassDao;
import com.dao.smallClassDao;
import com.dao.userDao;
import com.entity.bigClass;
import com.entity.user;

public class mainServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//处理字符编码
		
		//处理请求
		String action=request.getParameter("action");
		
		userDao usd=new userDao();
		bigClassDao bcd=new bigClassDao();
		smallClassDao scd=new smallClassDao();
		
		if(action.equals("FindAll_UserServlet")){
			/**
			 * 展示用户
			 */
			String pagenow=request.getParameter("num");
			if(pagenow==null){
				pagenow="1";
			}
			int pagenum=new Integer(pagenow);
			
			if(pagenum<1){
				pagenum=1;
			};
			int pagesize=8;//每次读取的数据条数
			int count=usd.countUser();//数据库内数据总条数
			int pagecount=(count/pagesize==0)?(count/pagesize):(count/pagesize+1);//获得总页数
			if(pagenum>pagecount){
				pagenum=pagecount;
			}
			int pagestart=pagesize*(pagenum-1);//获得查询起始数据
			List list=usd.queryByNum(pagestart, pagesize);
			request.getSession().setAttribute("pagenow", pagenum);
			request.setAttribute("pagecount", pagecount);
			request.setAttribute("pagenow", pagenum);
			request.setAttribute("list", list);
			request.getRequestDispatcher("findall_user.jsp").forward(request, response);
		}else if (action.equals("FindForUpdate_UserServlet")){
			/**
			 * 根据id查询用户
			 */
			String id=request.getParameter("id");
			user user=usd.queryById(id);
			request.setAttribute("ad", user);
			request.getRequestDispatcher("update_user.jsp").forward(request, response);
		}else if (action.equals("Update_UserServlet")){
			/**
			 * 根据id修改用户
			 */
			String id=request.getParameter("id");
			String user=request.getParameter("user");
			String name=request.getParameter("name");
			String pwd=request.getParameter("pwd");
			String sex=request.getParameter("sex");
			String birthday=request.getParameter("birthday");
			String personId=request.getParameter("personId");
			String email=request.getParameter("email");
			String phone=request.getParameter("phone");
			String address=request.getParameter("address");
			user us=new user(id,name,user,pwd,email,sex,birthday,phone,address,personId);
			int k=usd.updateByManager(us);
			if (k!=0){
				request.setAttribute("mess", "修改成功!");
			}else{
				request.setAttribute("mess", "修改失败!");
			}
			Object num=request.getSession().getAttribute("pagenow");
			request.getRequestDispatcher("main?action=FindAll_UserServlet&num="+num).forward(request, response);
		}else if(action.equals("Delete_UserServlet")){
			/**
			 * 根据id删除用户
			 */
			String id=request.getParameter("id");
			int k=usd.delById(id);
			if (k!=0){
				request.setAttribute("mess", "删除成功!");
			}else{
				request.setAttribute("mess", "删除失败!");
			}
			Object num=request.getSession().getAttribute("pagenow");
			request.getRequestDispatcher("main?action=FindAll_UserServlet&num="+num).forward(request, response);
		}else if(action.equals("checkUser")){
			/*
			 *查验用户账号是否存在
			 */
			String user=request.getParameter("user");
			boolean bn=usd.checkUser(user);
			if(bn){
				response.getWriter().write("×");
			}else{
				response.getWriter().write("√");
			}
			
		}else if(action.equals("Add_UserServlet")){
			/*
			 * 添加用户信息
			 */
			String user=request.getParameter("user");
			String name=request.getParameter("name");
			String pwd=request.getParameter("pwd");
			String sex=request.getParameter("sex");
			String birthday=request.getParameter("birthday");
			String personId=request.getParameter("idcard");
			String email=request.getParameter("email");
			String phone=request.getParameter("tel");
			String address=request.getParameter("address");
			user us=new user(name, user, pwd, email, sex, "", "", birthday, phone, address, personId);
			int k=usd.add(us);
			if(k!=0){
				request.setAttribute("mess", "添加成功!");
			}else{
				request.setAttribute("mess", "添加失败!");
			}
			int pagesize=8;//每次读取的数据条数
			int count=usd.countUser();//数据库内数据总条数
			int pagecount=(count/pagesize==0)?(count/pagesize):(count/pagesize+1);//获得总页数
			request.getRequestDispatcher("main?action=FindAll_UserServlet&num="+pagecount).forward(request, response);
		}else if(action.equals("AddBigClassServlet")){
			/*
			 * 添加大分类
			 */
			String name=request.getParameter("name");
			bigClass bc=new bigClass(name);
			int k=bcd.add(bc);
			if(k!=0){
				request.setAttribute("mess", "添加成功!");
			}else {
				request.setAttribute("mess", "添加失败!");
			}
			request.getRequestDispatcher("add_bigclass.jsp").forward(request, response);
		}else if(action.equals("checkBigClassName")){
			/*
			 * 大分类添加验证
			 */
			String classname=request.getParameter("classname");
			boolean bn=bcd.checkName(classname);
			if(bn){
				response.getWriter().write("该分类已经存在!");
			}else{
				response.getWriter().write("√");
			}
		}else if(action.equals("findallbigClass")){
			/*
			 * 分页查询大分类
			 */
			String pagenow=request.getParameter("num");
			if(pagenow==null){
				pagenow="1";
			}
			int pagenum=Integer.parseInt(pagenow);
			if (pagenum<1){
				pagenum=1;
			}
			int pagesize=8;
			int count=bcd.count();
			int pagecount=(count/pagesize==1)?(count/pagesize):(count/pagesize+1);
			if(pagenum>pagecount){
				pagenum=pagecount;
			}
			int pagestart=pagesize*(pagenum-1);
			List list=bcd.queryByNum(pagestart, pagesize);
			request.setAttribute("list", list);
			request.setAttribute("pagecount", pagecount);
			request.setAttribute("pagenow", pagenum);
			request.getSession().setAttribute("pagenow", pagenum);
			request.getRequestDispatcher("findall_bigclass.jsp").forward(request, response);
		}else if(action.equals("FindForUpdateBigClassServlet")){
			/*
			 * 查查询单个大分类
			 */
			String id=request.getParameter("id");
			bigClass bc=bcd.queryById(id);
			request.setAttribute("bc", bc);
			request.getRequestDispatcher("update_bigclass.jsp").forward(request, response);
		}else if(action.equals("UpdateBigClassServlet")){
			/*
			 * 修改单个大分类
			 */
			String id=request.getParameter("id");
			String name=request.getParameter("name");
			bigClass bc=new bigClass(id, name);
			int k=bcd.updateById(bc);
			if(k!=0){
				request.setAttribute("mess", "修改成功!");
			}else{
				request.setAttribute("mess", "修改失败!");
			}
			int pagenum=(Integer) request.getSession().getAttribute("pagenow");
			request.getRequestDispatcher("main?action=findallbigClass&num="+pagenum).forward(request, response);
		}/*else if(action.equals("DeleteBigClassServlet")){
			
			 * 删除单个大分类
			 
			String id=request.getParameter("id");
			//1.查询该分类是否有外键存在
			boolean bn=scd.checkBid(id);
			int pagenum=(Integer) request.getSession().getAttribute("pagenow");
			if(bn){
				request.setAttribute("mess", "删除失败!该分类包含其他的商品类别!");
			}else{
				int k=bcd.delById(id);
				if(k!=0){
					request.setAttribute("mess", "删除成功!");
				}else{
					request.setAttribute("mess", "删除失败!");
				}
			}
			request.getRequestDispatcher("main?action=findallbigClass&num="+pagenum).forward(request, response);
		}*/else if(action.equals("Ajax_SmallServlet")){
			/*
			 * 查询大分类是否有外键存在
			 */
			String id=request.getParameter("id");
			boolean bn=scd.checkBid(id);
			if(bn){
				response.getWriter().write("×");
			}
		}else if(action.equals("DeleteBigClassServlet")){
			String id=request.getParameter("id");
			int k=bcd.delById(id);
			if(k!=0){
				request.setAttribute("mess", "删除成功!");
			}else{
				request.setAttribute("mess", "删除失败!");
			}
			int pagenum=(Integer) request.getSession().getAttribute("pagenow");
			request.getRequestDispatcher("main?action=findallbigClass&num="+pagenum).forward(request, response);
		}
		
		//处理数据库
		
		//响应 页面跳转
		
		
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
